/*
 * @Descripttion:
 * @version:
 * @Author: mmzhang
 * @Date: 2021-07-27 16:02:34
 * @LastEditTime: 2022-03-31 14:55:10
 */
let vm = null
import clueCustomerMgr from '@/biz/customer/clueCustomer.js'
import CustomerMgr from '@/biz/customer/customerOpt.js'
import dateOpt from '@/biz/option/date.js'
import table from '@/components/dt/table/render.js'
import dt from '@dt/dt'
import nameOpt from '@/biz/option/name.js'
import channelMgr from '@/biz/channel/channel'
import standBookListOpt from '@/biz/standBookList/standBookListOpt.js'
import callPop from '../../../casemanagement/allCustomersManagement/components/callRecordPoptip.vue'
import poptipContent from '../../../casemanagement/allCustomersManagement/components/poptipContent.vue'
import protectOption from '@/biz/protectOption/protectOption'
import customerOptMgr from '@/biz/customer/customerOpt.js'
let customerStatusOptions = [
  { key: '首访', value: '首访' },
  { key: '回笼', value: '回笼' },
  { key: '认购', value: '认购' },
  { key: '签约', value: '签约' },
  { key: '全款', value: '全款' },
  { key: '交房', value: '交房' },
  { key: '未到访', value: '未到访' }
]

function options(_this, head) {
  vm = _this

  return [
    {
      field: 'customerPhone',
      minWidth: 130,
      fixed: 'left',
      header: {
        filter: {
          title: '手机号码',
          key: 'customerPhone',
          placeholder: '请输入手机号码',
          input: {}
        }
      }
    },
    {
      field: 'wechat',
      title: '微信号',
      minWidth: 160,
      fixed: 'left',
      header: {
        filter: {
          title: '微信号',
          key: 'wechat',
          placeholder: '请输入微信号',
          input: {}
        }
      }
    },
    {
      field: 'customerName',
      title: '客户姓名',
      minWidth: 120,

      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {}
        }
      }
    },
    {
      field: 'statusName',
      minWidth: 120,
      title: '客户进度',
      header: {
        filter: {
          title: '客户进度',
          key: 'statusNames',
          placeholder: '请选择客户进度',
          load: (o) => {
            o.select.options = customerStatusOptions
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    {
      field: 'divisionName',
      minWidth: 180,
      title: '所属事业部'
    },
    {
      field: 'companyName',
      minWidth: 180,
      title: '所属子公司'
    },
    {
      field: 'officeName',
      minWidth: 180,
      title: '所属事业处'
    },
    {
      field: 'buildingName',
      minWidth: 180,
      title: '所属案场'
    },
    {
      field: 'visitCount',
      minWidth: 120,
      title: '到访次数'
    },
    {
      field: 'orderCount',
      minWidth: 120,
      title: '成交套数'
    },
    {
      field: 'attributionConsultant',
      minWidth: 120,
      header: {
        filter: {
          title: '归属顾问',
          key: 'userId',
          placeholder: '请选择归属顾问',
          // load: (o) => {
          //   o.select.options = clueCustomerMgr.selectTeamUser()
          // },
          select: {
            options: [],
            // multiple: true,
            filterable: true,
            remote: (key) => {
              return clueCustomerMgr.selectTeamUser(key, false)
            }
          }
        }
      }
    },
    {
      //3.0增
      field: 'clueSourceChannelName',
      minWidth: 150,
      title: '线索来源渠道',
      header: {
        filter: {
          title: '线索来源渠道',
          key: 'clueChannelIds',
          placeholder: '请选择线索来源渠道',
          load: (o) => {
            o.select.options = clueCustomerMgr.channelSource()
          },
          change: (v) => {
            console.log(v.editValue, 'vvvvvvvvvvvvvvvv')
            _this.channelChange(v.editValue)
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
      // slots: {
      //   default: (params, h) => {
      //     let arrClueChannel = []
      //     if (params.row.clueSourceChannel) {
      //       JSON.parse(params.row.clueSourceChannel).forEach(item => {
      //         arrClueChannel.push(item.channelName)
      //       })
      //     }
      //     return table.cell.text(h, {
      //       value: arrClueChannel && arrClueChannel.length > 0 ? arrClueChannel
      //         .length > 1 ? `${arrClueChannel[0]}等${arrClueChannel.length}个` :
      //         arrClueChannel[0] : '-'
      //     })
      //   }
      // }
    },
    {
      field: 'channelSourceName',
      minWidth: 120,
      title: '到访渠道'
    },
    {
      title: '自获客打法',
      field: 'planName',
      minWidth: 160,
      align: 'center',
      header: {
        filter: {
          title: '自获客打法',
          key: 'planNameval',
          placeholder: '请选择',

          load: (o) => {
            let params = {}
            customerOptMgr.planList(params).then((res) => {
              console.log(res, 'fdfffffffffffff')
              res.forEach((item) => {
                item.key = item.id
                item.value = item.planName
              })
              o.select.options = res
            })
          },
          select: {
            options: [],
            multiple: true,
            filterable: true
          }
        }
      }
    },
    {
      field: 'createTime',
      minWidth: 200,
      title: '创建日期',
      header: {
        filter: dateOpt.range({
          title: '创建日期',
          placeholder: '请选择创建日期',
          startKey: 'createStartTime',
          endKey: 'createEndTime',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (vm.$tool.dateDiff(val.createStartTime, val.createEndTime).Y >= 2) {
              // 日期最大跨度两年
              vm.$Message.warning('认购日期跨度至多两年。')
            }
            return !(vm.$tool.dateDiff(val.createStartTime, val.createEndTime).Y >= 2)
          }
        })
      }
    },
    {
      field: 'lastVisitTime',
      minWidth: 200,
      title: '最近到访日期',
      header: {
        filter: dateOpt.range({
          title: '最近到访日期',
          placeholder: '请选择最近到访日期',
          startKey: 'lastVisitStartTime',
          endKey: 'lastVisitEndTime',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (vm.$tool.dateDiff(val.lastVisitStartTime, val.lastVisitEndTime).Y >= 2) {
              // 日期最大跨度两年
              vm.$Message.warning('认购日期跨度至多两年。')
            }
            return !(vm.$tool.dateDiff(val.lastVisitStartTime, val.lastVisitEndTime).Y >= 2)
          }
        })
      }
    },
    {
      field: 'lastFollowUpTime',
      minWidth: 200,
      title: '最近跟进时间',
      header: {
        filter: dateOpt.range({
          title: '最近跟进时间',
          placeholder: '请选择最近跟进时间',
          startKey: 'lastFollowUpStartTime',
          endKey: 'lastFollowUpEndTime',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (vm.$tool.dateDiff(val.lastFollowUpStartTime, val.lastFollowUpEndTime).Y >= 2) {
              // 日期最大跨度两年
              vm.$Message.warning('认购日期跨度至多两年。')
            }
            return !(vm.$tool.dateDiff(val.lastFollowUpStartTime, val.lastFollowUpEndTime).Y >= 2)
          }
        })
      }
    },
    {
      field: 'customerFeature',
      minWidth: 180,
      title: '客户特征'
    },
    {
      field: 'firstContactDate',
      minWidth: 200,
      title: '首触日期'
    },
    {
      field: 'sourceTypeName',
      minWidth: 120,
      title: '创建方式'
    },
    {
      field: 'checkInMethodName',
      minWidth: 120,
      title: '签到方式'
    },
    {
      field: 'featurePhoto',
      minWidth: 180,
      title: '特征附件',
      slots: {
        default: (params, h) => {
          // console.log(params.row.featurePhoto,'params.row.featurePhotoparams.row.featurePhotoparams.row.featurePhoto');
          // let img = {
          //   event: 'lookImg',
          //   // src:params.row&&params.row.featurePhoto&&params.row.featurePhoto.length>0&&params.row.featurePhoto[0] ? params.row.featurePhoto[0] : '',
          //   // data: params.row.featurePhoto
          // }
          // return table.cell.img(_this, h, img, true)
          let img = {
            event: 'lookImg',
            src: params.row.featurePhoto ? params.row.featurePhoto[0] : '',
            data: params.row.featurePhoto ? params.row.featurePhoto : ''
          }
          return params.row.featurePhoto ? table.cell.img(_this, h, img, true) : '-'
        }
      }
    },
    {
      field: 'isChannelName',
      minWidth: 120,
      title: '是否渠道'
      // slots: {
      //   default: (params, h) => {
      //     return params.row.isChannel == 1 ? '是' : "否"
      //   }
      // }
    },
    {
      field: 'channelTakeLook',
      minWidth: 120,
      title: '渠道带看单'
    },
    {
      field: 'recommender',
      minWidth: 120,
      title: '推荐人',
      header: {
        filter: nameOpt.createPushPeople(
          {
            key: 'recommender',
            title: '推荐人',
            to: (info) => {
              return {
                recommenderId: info.userId
              }
            }
          },
          'onCase'
        )
      }
      // header: {
      //   filter: nameOpt.createPushPeople({
      //     key: 'recommenderId',
      //     title: '推荐人'
      //     // to: (info) => {
      //     //   return {
      //     //     recommenderName: info.name.split('(')[0]
      //     //   }
      //     // }
      //   })
      // }
    },
    {
      field: 'wechatPhoto',
      minWidth: 180,
      title: '微信附件'
    },
    {
      field: 'isEffectiveName',
      minWidth: 180,
      title: '是否有效'
      // slots: {
      //   default: (params, h) => {
      //     return params.row.isChannel == 1 ? '有效' : params.row.isChannel == 2 ? "无效" : '失效'
      //   }
      // }
    },
    {
      title: '营销工具',
      field: 'marketingTool',
      minWidth: 160,
      align: 'center',
      header: {
        filter: {
          title: '营销工具',
          key: 'marketingTools',
          placeholder: '请选择',
          select: {
            multiple: true,
            options: dt.dictInfo().marketingTool
          }
        }
      }
    },
    {
      field: 'marketingActivity',
      title: '营销活动',
      minWidth: 200,
      header: {
        filter: {
          title: '营销活动',
          key: 'marketingActivity',
          placeholder: '请输入营销活动',
          input: {}
        }
      }
    },
    {
      title: '线索授权位置',
      field: 'authLocation',
      minWidth: 160,
      align: 'center',
      header: {
        filter: {
          title: '线索授权位置',
          key: 'authLocation',
          placeholder: '请选择',
          select: {
            multiple: true,
            options: dt.dictInfo().authLocation
          }
        }
      }
    },
    {
      field: 'handles',
      title: '操作',
      width: 180,
      fixed: 'right',
      align: 'center',
      className: 'conciseContent',
      slots: {
        default: (params, h) => {
          return [
            h(
              'div',
              {
                style: {
                  display: 'flex',
                  flexDirection: 'column'
                }
              },
              [
                h(
                  'p',
                  {
                    style: {
                      // display: 'flex',
                      display: params.row.callRecordNum ? 'flex' : 'block',
                      flexDirection: 'row'
                    }
                  },
                  [
                    h(
                      'Button',
                      {
                        props: {
                          type: 'text'
                        },
                        style: {
                          cursor: 'pointer',
                          padding: '0 10px'
                        },
                        on: {
                          click: (e) => {
                            e.stopPropagation()
                            vm.viewClick(params.row)
                          }
                        }
                      },
                      '查看详情'
                    ),
                    h(
                      'Poptip',
                      {
                        props: {
                          placement: 'left',
                          transfer: true,
                          width: '450',
                          trigger: 'click',
                          confirm: true,
                          // okText: vm.popLen == 0 ?
                          //   '' : `查看全部${vm.popLen}条记录`,
                          cancelText: '关闭',
                          popperClass: 'M_poptip' + ' ' + vm.callPopClass
                        },
                        style: {
                          display: params.row.callRecordNum ? 'block' : 'none'
                        }

                        // on: {
                        //   'on-ok': () => {
                        //     vm.viewClick(params.row, true)
                        //   },
                        // },
                      },
                      [
                        h(
                          'Button',
                          {
                            props: {
                              type: 'text'
                            },
                            style: {
                              color: params.row.newNews == '1' ? '#ed4014' : '#0F42A7',
                              cursor: 'pointer',
                              padding: '0 10px'
                            },
                            on: {
                              click: (e) => {
                                vm.getCallList(params.row)
                              }
                            }
                          },
                          params.row.callRecordNum ? `电话(${params.row.callRecordNum})` : ``
                        ),
                        h(callPop, {
                          slot: 'title',
                          props: {
                            callList: vm.callList,
                            popData: params.row,
                            callPopLoading: vm.callPopLoading
                          },

                          on: {
                            // popClose: (val) => {
                            //   vm.popClass = 'popClass'
                            // },
                            // initList: (val) => {
                            //   vm.initData()
                            // },
                          }
                        })
                      ]
                    )
                  ]
                ),

                h(
                  'Poptip',
                  {
                    props: {
                      transfer: true,
                      width: '380',
                      trigger: 'click',
                      confirm: true,
                      okText: vm.popLen == 0 ? '' : `查看全部${vm.popLen}条记录`,
                      cancelText: '关闭',
                      popperClass: 'M_poptip' + ' ' + vm.popClass
                    },

                    on: {
                      'on-ok': () => {
                        vm.viewClick(params.row, true)
                      }
                    }
                  },
                  [
                    h(
                      'Button',
                      {
                        props: {
                          type: 'text'
                        },
                        style: {
                          color: params.row.newNews == '1' ? '#ed4014' : '#0F42A7',
                          cursor: 'pointer',
                          padding: '0 10px'
                        },
                        on: {
                          click: (e) => {
                            vm.getFollowUp(params.row)
                          }
                        }
                      },
                      params.row.newNews == '1'
                        ? '有新的跟进'
                        : `跟进(${params.row.followNumber})&批注(${params.row.annotationNumber})`
                    ),
                    h(poptipContent, {
                      slot: 'title',
                      props: {
                        followUpList: vm.followUpList,
                        popData: params.row,
                        popLoading: vm.popLoading
                      },

                      on: {
                        popClose: (val) => {
                          vm.popClass = 'popClass'
                        },
                        initList: (val) => {
                          vm.initData()
                        }
                      }
                    })
                  ]
                )
              ]
            )
          ]
        }
      }
    }
  ]
}
export default {
  options
}
